import axios from 'axios'
// import { Message } from 'element-ui'
import store from '@/store'

const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 5000 // request timeout
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    // 统一注入 token
    const token = store.state.user.userToken
    if (token) {
      // 需要拼接后端要求的固定标识 'Bearer xxx'
      config.headers.Authorization = `${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    // console.log(response)
    // const { success, msg } = response.data
    // if (success) {
    //   return response
    // } else {
    //   Message.error(msg)
    //   return Promise.reject(msg)
    // }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

export default request
